﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JasRibbonApplicatie.Model;

namespace JasRibbonApplicatie.Views
{
    /// <summary>
    /// Interaction logic for ToebehorenLinkMateriaal.xaml
    /// </summary>
    public partial class ToebehorenLinkMateriaal : Page
    {
        
        BeheerMateriaal beheermateriaal = Login.Instance.MainWindowReference.currentPage as BeheerMateriaal;
        Materiaaltype materiaal = null;
        List<ToebehorenMateriaal> links;
        private static MateriaalModel mm = new MateriaalModel();

        public ToebehorenLinkMateriaal()
        {
            InitializeComponent();
            LinkMinButton.IsEnabled = false;
            toebehorenListBox.ItemsSource = mm.GetAllToebehorenTypes();
            //set materiaal
            setMateriaal();
        }

        internal void setMateriaal()
        {
            LinkButton.IsEnabled = false;
            Object item = beheermateriaal.materiaalTreeView.SelectedItem;
            if (item != null && item is Materiaaltype)
            {
                materiaal = item as Materiaaltype;
                links = mm.GetToebehorenMateriaal(materiaal);
                LinkListBox.ItemsSource = links;
            }
            else
            {
                materiaal = null;
            }
        }


        private void LinkButton_Click(object sender, RoutedEventArgs e)
        {
            if (materiaal != null)
            {
                ToebehorenType toebehoren = toebehorenListBox.SelectedItem as ToebehorenType;
                List<ToebehorenMateriaal> toebm = (from tm in links
                                                   where tm.toebehorenType_id == toebehoren.toebehorenType_id
                                                   select tm).ToList();
                if (toebm.Count() > 0)
                {
                    toebm.First().aantal++;
                }
                else
                {
                    ToebehorenMateriaal tm = new ToebehorenMateriaal()
                    {
                        materiaalType_id = materiaal.materiaalType_id,
                        toebehorenType_id = toebehoren.toebehorenType_id,
                        aantal = 1
                    };
                    links.Add(tm);
                    mm.AddToebehorenMateriaal(tm);
                }
                LinkMinButton.IsEnabled = true;
            }
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            mm.OpslaanWijzigingen(false);
            beheermateriaal.FillNodes();
        }

        private void LinkMinButton_Click(object sender, RoutedEventArgs e)
        {
            if (LinkListBox.SelectedItem != null && LinkListBox.SelectedItem is ToebehorenMateriaal)
            {
                ToebehorenMateriaal toebm = LinkListBox.SelectedItem as ToebehorenMateriaal;
                if (toebm.aantal > 1)
                {
                    toebm.aantal--;
                }
                else if (toebm.aantal <= 1)
                {
                    links.Remove(toebm);
                    mm.RemoveToebehorenMateriaal(toebm);
                    LinkMinButton.IsEnabled = false;
                }
            }
        }

        private void toebehorenListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LinkButton.IsEnabled = (toebehorenListBox.SelectedItem != null && materiaal != null);
        }

        private void LinkListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LinkMinButton.IsEnabled = (LinkListBox.SelectedItem != null);
        }
    }
}
